//用户管理模块

var async=require('async');
var Food=require('../../model/Food');
var Shop=require('../../model/Shop');


//获取菜单列表
var getFoodList=function(data,callback){
	Food.all(function(err,foods){
		if(err) return callback(err);
		data["foods"]=[];
		for(var i=0;i<foods.length;i++){
			data["foods"].push(JSON.parse(foods[i]));
		}
		callback(null,data);
	});
};

//菜单管理首页
exports.list=function(req,res){
	var data={};
	//用户数据
	var name=req.session.user.name||'admin';
	var user={
		name:name
	}
	data["user"]=user;

	//操作提醒
	var operate={"success":false,"msg":""};
	if(req.session&&req.session.operate!=null) {
		operate=req.session.operate;
		delete req.session.operate;
	}
	data["operate"]=operate;

	data["foods"]=[];

	data["shopes"]=[];

	//获取商家列表
	var getShopList=function(callback){
		Shop.all(function(err,shopes){
			if(err) return callback(err);
			data["shopes"]=[];
			for(var i=0;i<shopes.length;i++){
				data["shopes"].push(JSON.parse(shopes[i]));
			}
			callback(null,data);
		})
	};

	//根据id获取商家信息
	var getShopInfoById=function(shopes,shopId){
		for(var i=0;i<shopes.length;i++){
			if(shopes[i].id==shopId){
				return shopes[i];
			}
		}
	}


	async.waterfall([getShopList,getFoodList],function(err,result){
		data=result||data;
		var foods=data["foods"];
		var shopes=data["shopes"];
		
		for(var i=0;i<foods.length;i++){
			data["foods"][i].shop=getShopInfoById(shopes,foods[i].shopId)["name"];
		}
		console.log("data:"+JSON.stringify(data));
		res.render('admin/food/overview',data); 
	});
}

//菜单创建页面
exports.createView=function(req,res){
	var data={};
	
	//用户数据
	//用户数据
	var name=req.session.user.name||'admin';
	var user={
		name:name
	}
	data["user"]=user;

	//操作提醒
	var operate={"success":false,"msg":""};
	if(req.session&&req.session.operate!=null) {
		operate=req.session.operate;
		delete req.session.operate;
	}
	data["operate"]=operate;
	//标识是否是编辑页面
	data["edit"]=false;
	//店铺数据
	data["shopes"]=[];

	Shop.all(function(err,shopes){
		shopes=shopes||[];
		for(var i=0;i<shopes.length;i++){
			data["shopes"].push(JSON.parse(shopes[i]));
		}
		console.log("ssssdata:"+JSON.stringify(data));
		res.render('admin/food/create_edit',data);
	});
}

//菜单创建操作
exports.create=function(req,res){
	console.log("body:"+JSON.stringify(req.body));
	var name=req.body.name.trim();
	var price=req.body.price.trim();
	var shopId=req.body.shopId;

	var food=new Food(name,price,shopId);

	//插入数据
	food.insert(function(err){
		if(err) {
			console.log("插入数据失败");
			req.session.operate={"success":false,"msg":"添加套餐信息失败!"};
			res.redirect('/admin/food/create');
		}else{
			console.log("插入数据成功");
			req.session.operate={"success":true,"msg":"已成功添加套餐信息!"};
			res.redirect('/admin/food');
		}
	})
}


//菜单编辑页面
exports.editView=function(req,res){
	var fId=parseInt(req.params.id);

	var data={};
	//用户数据
	var name=req.session.user.name||'admin';
	var user={
		name:name,
	}
	data["user"]=user;
		
	//操作提醒
	var operate={"success":false,"msg":""};
	if(req.session&&req.session.operate!=null) {
		operate=req.session.operate;
		delete req.session.operate;
	}
	data["operate"]=operate;
	//标识是否是编辑页面
	data["edit"]=true;
	data["shopes"]=[];

	//获取用户信息
	var getFoodInfoById=function(callback){
		Food.findById(fId,function(err,food){
			if(err) return callback(err);
			console.log("food:"+food);
			data["editFood"]=JSON.parse(food);
			callback(null,data);
		})
	}

	//获取商家列表
	var getShopList=function(data,callback){
		Shop.all(function(err,shopes){
			if(err) return callback(err);
			data["shopes"]=[];
			for(var i=0;i<shopes.length;i++){
				data["shopes"].push(JSON.parse(shopes[i]));
			}
			callback(null,data);
		})
	};

	async.waterfall([getFoodInfoById,getShopList],function(err,result){
		if(err) return res.send('数据错误');
		console.log("data:"+JSON.stringify(data));
		res.render('admin/food/create_edit',data);
	});
}

//菜单编辑操作
exports.edit=function(req,res){
	console.log("body:"+JSON.stringify(req.body));
	var fId=parseInt(req.params.id);
	var name=req.body.name.trim();
	var price=req.body.price.trim();
	var shopId=req.body.shopId;

	var food=new Food(name,price,shopId);

	food.update(fId,function(err){
		if(err) {
			console.log("更新数据失败");
		    req.session.operate={"success":false,"msg":"更新套餐信息失败!"};
			res.redirect('/admin/food/edit/'+fId);
		}else{
			console.log("更新数据成功");
			req.session.operate={"success":true,"msg":"已成功保存套餐信息!"};
			res.redirect('/admin/food');
		}
    })
}


//菜单删除页面
exports.deleteView=function(req,res){
	var fid=parseInt(req.params.id);

	//获取菜单信息
	var getFoodInfoById=function(callback){
		Food.findById(fid,function(err,food){
			if(err) return callback(err);
			var data={};
			console.log("food:"+food);
			data["editFood"]=JSON.parse(food);
			callback(null,data);
		})
	}

	async.waterfall([getFoodInfoById],function(err,data){
		if(err) return res.send('数据错误');

		//用户数据
		var name=req.session.user.name||'admin';
		var user={
			name:name
		}
		data["user"]=user;

		//操作提醒
		var operate={"success":false,"msg":""};
		if(req.session&&req.session.operate!=null) {
			operate=req.session.operate;
			delete req.session.operate;
		}
		data["operate"]=operate;
		res.render('admin/food/delete',data);
	});

}

//菜单删除操作
exports.delete=function(req,res){
	var fid=parseInt(req.params.id);

	Food.deleteById(fid,function(err){
		if(err) {
			console.log("删除数据失败");
			req.session.operate={"success":false,"msg":"删除套餐信息失败!"};
			res.redirect('/admin/food/delete/'+uid);
		}else{
			console.log("删除数据成功");
			req.session.operate={"success":true,"msg":"删除套餐信息成功!"};
			res.redirect('/admin/food');
		}	
	})
}


